package com.fwzc.giuseppe.activity;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import com.fwzc.giuseppe.R;
import com.fwzc.giuseppe.Api.Api;
import com.fwzc.giuseppe.adapter.UniversalAdapter;
import com.fwzc.giuseppe.base.BaseActivity;
import com.fwzc.giuseppe.entity.Logistic;
import com.fwzc.giuseppe.util.OrderState;
import com.fwzc.giuseppe.util.ViewHolder;
import com.lidroid.xutils.BitmapUtils;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.bitmap.BitmapDisplayConfig;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest;
/**
 * 退换货的进度查看
 * @author llf
 *
 */
public class ProgressActivity extends BaseActivity implements OnClickListener{
	private ListView progress;
	private UniversalAdapter<Logistic> adapter;
	private List<Logistic> datas = new ArrayList<Logistic>();
	private ImageView image;
	private TextView name,order_code,order_price;
	private Button order_state;
	private String userId,brief,orderNumber,price,imgURL,orderdetailid;
	private int state;
	private ProgressDialog load;
	private HttpUtils httpUtil;
	private BitmapUtils bitmapUtil;
	private SimpleDateFormat dataFormat; 
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_progress);
		
		initBar();
		initView();
		initListView();
	}
	
	@SuppressLint("SimpleDateFormat") 
	private void initView() {
		dataFormat = new SimpleDateFormat("yyyy-MM-dd");
		// 网络请求相关
		httpUtil = new HttpUtils();
		bitmapUtil = new BitmapUtils(this);
		load = new ProgressDialog(this);
		load.setMessage("数据加载中.....");
		load.setCancelable(false);
		userId = getSharedPreferences("user", Activity.MODE_PRIVATE).getString(
				"userId", "");
		
		actionbar_side_name.setText("进度查询");
		iv_actionbar_side_left.setVisibility(View.VISIBLE);
		iv_actionbar_side_left.setOnClickListener(this);
		bg_actionbar_side.setBackgroundColor(getResources().getColor(R.color.gray_bg));

		progress = (ListView) findViewById(R.id.progress);
		image = (ImageView)findViewById(R.id.clothes_image);
		name = (TextView)findViewById(R.id.name);
		order_code = (TextView)findViewById(R.id.order_code);
		order_price = (TextView)findViewById(R.id.price);
		order_state = (Button)findViewById(R.id.state);
		
		Intent intent = getIntent();
		state = intent.getIntExtra("state", -1);
		brief = intent.getStringExtra("brief");
		orderNumber = intent.getStringExtra("orderId");
		price = intent.getStringExtra("price");
		imgURL = intent.getStringExtra("imgURL");
		orderdetailid = intent.getStringExtra("orderdetailid");
		name.setText(brief);
		order_code.setText("订单号:" + orderNumber);
		order_price.setText("￥"+price);
		if(state == OrderState.Return){
			order_state.setText("退货");
		}else{
			order_state.setText("换货");
		}
		BitmapDisplayConfig config = new BitmapDisplayConfig();
		config.setLoadFailedDrawable(getResources().getDrawable(R.drawable.error));
		bitmapUtil.display(image, imgURL , config);
	}

	private void initListView() {
//		for (int i = 0; i < 10; i++) {
//			Logistic logistic = new Logistic();
//			if (i == 0) {
//				logistic.last_operation = true;
//			} else {
//				logistic.last_operation = false;
//			}
//			logistic.operation = "您的包裹已经在北京海定区包装" + i;
//			logistic.time = "2015-06-25";
//			datas.add(logistic);
//		}
		
		adapter = new UniversalAdapter<Logistic>(this,datas,R.layout.logistic_item) {

			@Override
			public void convert(ViewHolder viewHolder, Logistic logistic) {
				ImageView point = (ImageView)viewHolder.getView(R.id.subsection_point);
				TextView operation = (TextView)viewHolder.getView(R.id.operation);
				TextView time = (TextView)viewHolder.getView(R.id.time);
				operation.setText(logistic.operation);
				time.setText(logistic.time);
				if(logistic.last_operation){
					point.setBackgroundResource(R.drawable.node_select);
					operation.setTextColor(getResources().getColor(R.color.pink));
				}else{
					point.setBackgroundResource(R.drawable.node_nomal);
					operation.setTextColor(getResources().getColor(R.color.time_line_text));
				}
			}
		};
		progress.setAdapter(adapter);
		getRequest();
	}
	
	private void getRequest(){
		RequestParams params = new RequestParams("UTF-8");
		params.addBodyParameter("userId", userId);
		params.addBodyParameter("orderId", orderNumber);
		if(state == OrderState.Return){
		params.addBodyParameter("type", "5");
		}else{
			params.addBodyParameter("type", "6");
		}
		params.addBodyParameter("orderGoodsDetailId", orderdetailid);
		httpUtil.send(HttpRequest.HttpMethod.POST, Api.PROGRESS,
				params, new RequestCallBack<String>() {

					@Override
					public void onStart() {
						load.show();
					}

					@Override
					public void onFailure(HttpException arg0, String response) {
						load.dismiss();
						showToast("加载数据失败");
					}

					@Override
					public void onSuccess(ResponseInfo<String> responseStr) {
						try {
							JSONObject response = new JSONObject(responseStr.result);
							int resultCode = response.getInt("resultCode");
							if(resultCode == 0){
								JSONObject joo = response.getJSONObject("dataList");
								JSONArray ja = joo.getJSONArray("orderProgreess");
								for (int i = ja.length()-1; i >=0; i--) {
									JSONObject jo = ja.getJSONObject(i);
									Logistic logistic = new Logistic();
									logistic.last_operation = (i==ja.length()-1);
									logistic.time = dataFormat.format(new Date(jo.getInt("timestamp")*1000));
									logistic.operation = jo.getString("brief");
									datas.add(logistic);
								}
								adapter.notifyDataSetChanged();
								load.dismiss();
							}else{
								load.dismiss();
								showToast(response.getString("msg"));
							}
						} catch (JSONException e) {
							load.dismiss();
							showToast("解析出错");
						}
					}
				});
	}
	
	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		// 返回
		case R.id.iv_actionbar_side_left:
			finish();
			break;

		default:
			break;
		}
	}
}
